﻿using System;
using System.Diagnostics;

namespace PBGenerator
{
    public class ProcessHelper
    {
        private Process process;

        public ProcessHelper(string appPath, string paramInfo)
        {
            this.process = new Process();
            this.process.StartInfo.FileName = appPath;
            this.process.StartInfo.Arguments = paramInfo;
            this.process.StartInfo.CreateNoWindow = true;
            this.process.StartInfo.UseShellExecute = false;
            this.process.StartInfo.RedirectStandardInput = true;
            this.process.StartInfo.RedirectStandardOutput = true;
            this.process.StartInfo.RedirectStandardError = true;
        }

        public void run()
        {
            this.process.Start();
            string processName = this.process.StartInfo.FileName;
            string processParam = this.process.StartInfo.Arguments;
            this.process.BeginOutputReadLine();
            this.process.BeginErrorReadLine();
            this.process.OutputDataReceived += new DataReceivedEventHandler(this.process_OutputDataReceived);
            this.process.ErrorDataReceived += new DataReceivedEventHandler(this.process_ErrorDataReceived);
            this.process.WaitForExit();
            Program.writeInfo(processName + " " + processParam + "执行完毕！！！");
            this.process.Close();
        }

        private void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            Program.writeInfo(e.Data);
        }

        private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Program.writeInfo(e.Data);
        }
    }
}
